import 'dart:io';
import 'dart:typed_data';

import 'package:android_intent_plus/android_intent.dart';
import 'package:r_album/r_album.dart';
import 'package:url_launcher/url_launcher.dart';

class Utils{
  ///获取ip
  static Future<String> getIp() async {
    for (var interface in await NetworkInterface.list()) {
      if (interface.name == "wlan0") {
        return interface.addresses[0].address;
      }
    }
    return "";
  }

  ///传入文件名(带格式) 判断是否是图片文件
  static bool isImgFile(String fileName) {
    return fileName.contains(".png") ||
        fileName.contains(".jpg") ||
        fileName.contains(".gif") ||
        fileName.contains(".webp");
  }

  // static saveScreen(List<int> byteDatas) async {
  //   if (byteDatas != null) {
  //     final result =
  //     await ImageGallerySaver.saveImage(Uint8List.fromList(byteDatas));
  //
  //     const AndroidIntent intent = AndroidIntent(
  //         action: 'android.intent.action.MAIN',
  //         category: 'android.intent.category.APP_GALLERY');
  //     await intent.launch();
  //     // _toastInfo(result.toString());
  //   }
  // }

  ///安卓端保存图片并跳转到相册
  static saveGalleryAndroid(List<int> byteDatas, String path) async {
    List<String> filePaths = [path];
    var isSuccess1 =
    await RAlbum.createAlbum("dddd").then((value) => print('$value'));
    var isSuccess = await RAlbum.saveAlbum("dddd", filePaths)
        .then((value) => print('$value'));

    const AndroidIntent intent = AndroidIntent(
        action: 'android.intent.action.MAIN',
        category: 'android.intent.category.APP_GALLERY');
    await intent.launch();
  }

  ///安卓端保存图片并跳转到相册
  static saveGalleryIOS(List<int> byteDatas, String path) async {
    List<String> filePaths = [path];
    var isSuccess1 =
    await RAlbum.createAlbum("dddd").then((value) => print('$value'));
    var isSuccess = await RAlbum.saveAlbum("dddd", filePaths)
        .then((value) => print('$value'));

    String _url = "photos-redirect://";
    if (await canLaunch(_url)) {
      launch(_url);
    }
  }
}